package com.chenxin.interact.process.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.interact.domain.wmsToMes.WmsToMesOverdue;
import com.chenxin.interact.domain.dto.IqcOutResponseDTO;
import com.chenxin.interact.domain.dto.IqcOverdueResponseDTO;
import com.chenxin.interact.mapper.WmsToMesOverdueMapper;
import com.chenxin.interact.process.IQmsInteractProcess;
import com.chenxin.wms.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QmsInteractProcessImpl implements IQmsInteractProcess {
    @Autowired
    private WmsToMesOverdueMapper wmsToMesOverdueMapper;
    @Autowired
    private StockMapper stockMapper;

    @Override
    public ProcResult procQmsOverdue(IqcOverdueResponseDTO dto) {
        if (dto == null) {
            return ProcResult.error("overdue_err#001", "参数不能为空");
        }
        // 报检单号不能为空
        String receiveRecordNo = dto.getReceiveRecordNo();
        if (StringUtils.isEmpty(receiveRecordNo)) {
            return ProcResult.error("overdue_err#002", "报检单号不能为空");
        }
        // 物料号不能为空
        String materialCode = dto.getMaterialCode();
        if (StringUtils.isEmpty(materialCode)) {
            return ProcResult.error("overdue_err#003", "物料号不能为空");
        }
        // 质检结果
        String inspectionResult = dto.getInspectionResult();
        if (StringUtils.isEmpty(inspectionResult)) {
            return ProcResult.error("overdue_err#004", "质检结果不能为空");
        }
        Date workDate = new Date();
        // 根据报检单号获取相关信息
        QueryWrapper<WmsToMesOverdue> recordNoQW = new QueryWrapper<>();
        recordNoQW.lambda().eq(WmsToMesOverdue::getReceiveRecordNo, receiveRecordNo).last("limit 1");
        WmsToMesOverdue overdue = wmsToMesOverdueMapper.selectOne(recordNoQW);
        if (overdue == null) {
            return ProcResult.error("overdue_err#003", "未匹配到报检单号【" + receiveRecordNo + "】的超期质检记录");
        }
        String message = "成功接收报检单号【" + receiveRecordNo + "】的质检结果为【" + inspectionResult + "】";
        // 更新接口状态为处理完成2
        overdue.setStatus(2);
        overdue.setUpdater(101L);
        overdue.setUpdateDate(workDate);
        wmsToMesOverdueMapper.updateById(overdue);
        return ProcResult.success(message);
    }

    @Override
    public ProcResult procQmsIqcOut(IqcOutResponseDTO dto) {
        // 接收到质检结果更新出库质检状态
        return ProcResult.success("成功接收质检结果");
    }
}
